﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using System.Text;
using StoragePeriod.Common;
using StoragePeriod.Web.Common;

namespace StoragePeriod.Web.SMS
{
    public partial class SMS : SecurityPage
    {
        #region 属性

        #endregion

        #region 继承
        public override string PrivCode()
        {
            return "30d8bd14467225b1";
        }

        #endregion

        #region 函数
        private void BindTreeView()
        {
            this.PersonnelTree.Nodes.Clear();
            List<OperationTeam> OperList = OperationTeam.All().ToList();
            foreach (OperationTeam Oper in OperList.Where(p => p.ParentId == 0))
            {
                TreeNode node = new TreeNode()
                {
                    Value = Oper.id.ToString(),
                    Text = Oper.OTName,
                    ImageUrl = "../images/WebResource.gif",
                    Target = "0",
                    SelectAction= TreeNodeSelectAction.None,
                    NavigateUrl="javascript:void(0);",
                    ShowCheckBox = false
                };
                foreach (OperationTeam Team in OperList.Where(p => p.ParentId == Oper.id))
                {
                    TreeNode c_node = new TreeNode()
                    {
                        Value = Team.id.ToString(),
                        Text = Team.OTName,
                        ImageUrl = "../images/person.gif",
                        Target = "0",
                        ShowCheckBox = true
                    };

                    List<SYS_Users> Userlist = SYS_Users.Where("UserType=@0", Team.id).ToList();
                    foreach (SYS_Users user in Userlist)
                    {
                        TreeNode p_node = new TreeNode()
                        {
                            Value = user.id.ToString(),
                            Text = user.UserName,
                            ImageUrl = "../images/WebResource1.gif",
                            Target = "1",
                            ShowCheckBox = true
                        };
                        c_node.ChildNodes.Add(p_node);
                    }
                    node.ChildNodes.Add(c_node);
                }
                this.PersonnelTree.Nodes.Add(node);
            }
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            this.BindTreeView();
        }

        protected void Btn_Select_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (TreeNode node in this.PersonnelTree.Nodes.AllNodes().Where(p => p.Checked == true && p.Target == "1"))
            {
                sb.Append(node.Text + ";");
            }
            this.txt_Staff.Text = sb.ToString();
        }

        protected void Btn_submint_Click(object sender, EventArgs e)
        {
            string script = "";
            if (this.PersonnelTree.Nodes.AllNodes().Where(p => p.Checked == true && p.Target == "1").Count() < 1)
            {
                script = "alert('请选择收件人！')";
                ScriptManager.RegisterStartupScript(this, Page.GetType(), "", script, true);
                return;
            }
            if (string.IsNullOrEmpty(this.txt_title.Text.Trim()))
            {
                script = "alert('主题不能为空！')";
                ScriptManager.RegisterStartupScript(this, Page.GetType(), "", script, true);
                return;
            }

            if (string.IsNullOrEmpty(this.txt_centent.Value.Trim()))
            {
                script = "alert('内容不能为空！')";
                ScriptManager.RegisterStartupScript(this, Page.GetType(), "", script, true);
                return;
            }

            foreach (TreeNode node in this.PersonnelTree.Nodes.AllNodes().Where(p => p.Checked == true && p.Target == "1"))
            {
                SYS_Users user = SYS_Users.Where("UserCode=@0", User.Identity.Name).First();

                SendList send = new SendList();
                send.SendPersonnelID = Convert.ToInt32(user.id);
                send.SendPersonnelName = user.UserName;
                send.PersonnelID = Convert.ToInt32(node.Value);
                send.PersonnelName = node.Text;
                send.Title = this.txt_title.Text.Trim();
                send.SendContent = this.txt_centent.Value.Trim();
                send.SendType = Convert.ToInt32(EnumData.SendType.短讯);
                send.SendDate = DateTime.Now;
                send.IsRead = false;
                send.ReceiverDel = false;
                send.SendDel = false;
                SendList.Insert(send);
            }
            //UtilityUI.Load(this.UpdatePanel1, "alert('发送成功!');window.location.href='SendSMS.aspx';");
            script = "alert('发送成功!');window.location.href='SendSMS.aspx';";
            ScriptManager.RegisterStartupScript(this, Page.GetType(), "", script, true);
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            string strSearch = this.txt_search.Text.Trim();
            if (strSearch == "") return;

            foreach (TreeNode node in this.PersonnelTree.Nodes.AllNodes().Where(p => p.Target == "1" && p.Text.Contains(strSearch)))
            {
                node.Checked = true;
                node.Parent.Expand();
                node.Parent.Parent.Expand();
            }
        }
        #endregion


    }
}